package android.FiltrosJava;

import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;

public abstract class Neighborhood extends AbstractImageBasic{
	protected int neighborhood = 3;
	protected int halfNeighborhood = (int) (neighborhood / 2.0f);
	
	public Neighborhood(int neighborhoodSize){
		setNeighborhood(neighborhoodSize);
	}
	
	public void setNeighborhood(int neighborhoodSize){
		neighborhood = Math.abs(neighborhoodSize);
		if ( neighborhood ==0)
			neighborhood = 3;
		
		halfNeighborhood = (int) (neighborhood / 2.0f);
	}
	
	public BufferedImage applyFilter(BufferedImage src){		
		BufferedImage dst = copiaImagem(src);//createCompatibleDestImage(src);
		
		int width = src.getWidth();
        int height = src.getHeight();            
    	    	    	
    	WritableRaster raster_src = src.getRaster();
    	WritableRaster raster_dst = dst.getRaster();
    	int numBands = raster_src.getNumBands();
    	int[] imageData_src = new int[numBands];
    	int[] imageData_dst = new int[numBands];
    	int px;
    	int py;
    	
    	for ( int y = 0; y < height; y++ ) {
    		for ( int x = 0; x < width; x++ ) {    			
    			raster_src.getPixel(x,y,imageData_src);
    			imageData_dst[0] = imageData_src[0];
    			imageData_dst[1] = imageData_src[1];
    			imageData_dst[2] = imageData_src[2];
    			
    			for (int my = 0; my < neighborhood; my++){
    				for (int mx = 0; mx < neighborhood; mx++){
    					px = x + mx - halfNeighborhood;
    					py = y + my - halfNeighborhood;
    					if ((px >=0) && (px < width) && (py >=0) && (py < height)){
    						raster_dst.getPixel(px,py,imageData_src);    						
    						imageData_dst = comparePixels(imageData_dst, imageData_src);    						
    					}
    				}
    			}				
				raster_src.setPixel(x,y,imageData_dst);				
    		}
    	}    	
		
		return src;
	}
	
	public abstract int[] comparePixels(int[] imageData, int[] newData);
	
}